<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/15
 * Time: 16:55
 * 品质考核
 */

namespace app\index\controller;


use think\Cookie;

class QualityExamine
{
    //品质考核
    const quality_examine = [
        'userId'=>'',           //用户id
        'projectName'=>'',      //考核名称
        'assessTime'=>'',       //考核时间
        'examiner'=>'',         //考核人
        'totalScore'=>'0',      //总分
        'status'=>'继续',       //完成状态
        'content'=>[],          //考核内容
    ];
    public static function getData($where, $one = true, $table = 'quality_examine')
    {
        return ($one == true) ? getFirst(mongo($table)->where($where)->select()) : mongo($table)->where($where)->select();
    }
    public static function setData($where, $data, $table = 'quality_examine')
    {
        mongo($table)->where($where)->update($data);
    }
    public static function insertData($data, $table = 'quality_examine')
    {
        mongo($table)->insert($data);
        return mongo($table)->getLastInsID();
    }
    public static function deleteData($where, $table = 'quality_examine')
    {
        mongo($table)->where($where)->delete();
    }

    /**
     *  新建考核详情
     * @param string $projectName 考核名称
     */
    public static function newAssessmentDetail($projectName = '')
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;

        $projectName = input('projectName');

        $insertData = [
            'userId'      => cookie('userId'),
            'examiner'    => User::getData(['id'=>cookie("userId")])['username'] ?? '',  //评分人
            'assessTime'  =>date('Y-m-d'),
            'projectName' =>$projectName,
        ];

        self::insertData(array_merge(self::quality_examine, $insertData));
        return RD(SUCCESS);
    }
    /**
     * 获取考核记录
     * @param string $year
     * @param string $month
     */
    public static function getAssessmentHistory($year = '', $month = '')
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;

        $year   = input('year');
        $month  = input('month');

        $result = self::getData(['assessTime'=>['like', $year.'-'.$month.'.*']], false);
        foreach ($result as $key=>$value) {
            DArray($result[$key], 'content');
        }
        return RD(SUCCESS, $result);
    }
    /**
     * 删除考核记录
     * @param string $id  考核记录id
     */
    public static function removeAssessment($id = '')
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;

        $id  = input('id');
        self::deleteData(['id'=>$id]);
        return RD(SUCCESS);
    }
    /**
     * 编辑考核详情信息
     * @param string $id      考核记录id
     * @param array $content  考核内容
     */
    public static function editAssessmentDetail($id = '', $content = [])
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;

        $id     = input('id');
        $content= input('content/a', []);

        $updateData = [
            'content'   => $content,
            'status'    => '查看',
            'totalScore'=>$content['totalScore']??'0',
        ];

        self::setData(['id'=>$id], $updateData);
        return RD(SUCCESS);
    }
    /**
     * 获取考核评分详情
     * @param string $id 考核记录id
     */
    public static function getAssessmentDetail($id = '')
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;

        $id     = input('id');
        return RD(SUCCESS, ['content'=>self::getData(['id'=>$id])['content'] ?? []]);
    }
}